﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Data;
using WPFExcelReport.Helpers;
using System.Windows;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    { 
        private void YGREKDOC()
        {
            try
            {
                if (!dsSource.Tables.Contains("EKKOPF"))
                {
                    return;
                }
                if (!dsSource.Tables.Contains("EKPOS"))
                {
                    return;
                }
                DataRow dwKK =dsSource.Tables["EKKOPF"].Rows[0];
                string value = "";
                int x = 1, y = 1;

                string liefnr = "";
                string payment = "";
                string cond = "";
                string filter = "";
                string order = "";
                string currency = dwKK["EKKWAEHRSL"].ToString();
                DataRow[] rows = null;
                liefnr = dwKK["EKKLIEFNR"].ToString();
                cond = dwKK["EKKKONDSL"].ToString();
                double total = System.Math.Round(System.Convert.ToDouble(dwKK["EKKWE1"].ToString()) * 0.01, 0);
                exAgent.SetCellValue(100, 31, total);
                order = dwKK["EKKAUFKRS"].ToString() + "-" + dwKK["EKKAUFLFD"].ToString();
                x = 5; y = 5;
                exAgent.SetCellValue(x, y, order);
                x = 3; y = 30;
                string str1 = "", str2 = "", str3 = "";
                Helper.GetSplitDate(ref str1, ref str2, ref str3, date);
                exAgent.SetCellValue(x, y, str3);
                filter = "LSLIEFNR='" + liefnr + "'";
                rows = dsSource.Tables["LIEFDAT"].Select(filter);
                foreach (DataRow RX in rows)
                {
                    value = RX["LSNAME1"].ToString().Trim();// +RX["LSNAME2"].ToString();
                    value = GetGB2312Value(value);
                    x = 4; y = 4;
                    exAgent.SetCellValue(x, y, value);
                    value = RX["LSSTRASSE1"].ToString();
                    value = GetGB2312Value(value);
                    x = 4; y = 13;
                    //exAgent.SetCellValue(x, y, value);
                }
                filter = "LSKLIEFNR='" + liefnr + "' AND LSKKONDSL='" + cond + "'";
                rows = dsSource.Tables["LIEFKO"].Select(filter);
                foreach (DataRow RX in rows)
                {
                    payment = RX["LSKZAHLBED"].ToString();
                }

                filter = "ZABNR='" + payment + "' and ZABSPRACHKZ='V'";
                rows = dsSource.Tables["ZABED"].Select(filter);
                foreach (DataRow RX in rows)
                {
                    exAgent.SetCellValue(140, 1, "2、付款方式:" + Helper.GetGB2312Value(RX["ZABBEZ"].ToString()));
                }
                var qz = from dwTemp in dsSource.Tables["TXTKON"].AsEnumerable()
                         where dwTemp["TXTIPIND"].ToString().Trim() == payment.Trim() + "V"
                         select dwTemp;
                foreach (DataRow row in qz)
                {
                    exAgent.SetCellValue(141, 1, "3、" + Helper.GetGB2312Value(row["TXT010"].ToString()));
                }
                int rown = -1;
                string artdes = "", colordes = "";
                int rpl = 4;
                int count = dsSource.Tables["ICC_EKPOS"].Rows.Count;
                foreach (DataRow dwEKPOS in dsSource.Tables["ICC_EKPOS"].Rows)
                {
                    rown++;
                    string artg = dwEKPOS["EKPARTGR"].ToString();
                    string art = dwEKPOS["EKPART"].ToString();
                    string var = dwEKPOS["EKPVAR"].ToString();
                    var = Left(var, 2);
                    string color = dwEKPOS["EKPFARBE"].ToString();
                    string size = dwEKPOS["EKPGROE"].ToString();
                    string cref = dwEKPOS["EKPLSGRPBEZ"].ToString().Trim();
                    string um = dwEKPOS["EKPMEEH"].ToString();
                    double qty = System.Math.Round(System.Convert.ToDouble(dwEKPOS["EKPME1"].ToString()) * 0.01, 2);
                    double tvalue = System.Math.Round(System.Convert.ToDouble(dwEKPOS["EKPWE1"].ToString()) * 0.01, 2);
                    double price = System.Math.Round(System.Convert.ToDouble(dwEKPOS["EKPPRME"].ToString()) * 0.001, 2);
                    value = dwEKPOS["EKPLTERM"].ToString();
                    x = 4; y = 30;
                    Helper.GetSplitDate(ref str1, ref str2, ref str3, value);
                    exAgent.SetCellValue(x, y, str3);
                    //exAgent.SetCellValue(x, y, value);
                    switch (artg)
                    {
                        case "1":
                            filter = "GSART='" + art + "' and GSVAR='" + var + "'";
                            rows = dsSource.Tables["GSDAT"].Select(filter);
                            foreach (DataRow rx in rows)
                            {
                                artdes = Helpers.Helper.GetGB2312Value(rx["GSGARNBEZ"].ToString()).Trim();
                            }
                            filter = "GSCART='" + art + "' and GSCVAR='" + var + "' AND GSCFARBE='" + color + "'";
                            rows = dsSource.Tables["GSCOLOR"].Select(filter);
                            foreach (DataRow rx in rows)
                            {
                                colordes = Helpers.Helper.GetGB2312Value(rx["GSCBEZ"].ToString()).Trim();
                            }
                            break;
                        case "2":
                            break;
                        case "3":
                            break;
                        case "4":
                            break;
                        case "5":
                            break;
                        case "6":
                            break;
                        case "7":
                            break;
                        case "8":
                            filter = "MSMAT='" + Left(art, 14) + "' and MSVAR='" + var + "'";
                            rows = dsSource.Tables["MSSTAMM"].Select(filter);
                            foreach (DataRow rw in rows)
                            {
                                artdes = GetGB2312Value(rw["MSBEZ"].ToString());
                            }
                            break;
                        default:
                            break;
                    }
                    x = 21 + rown * rpl; y = 1;
                    exAgent.SetCellValue(x, y, Helpers.Helper.GetSubRollNum(dwEKPOS["EKPAUFPOS"].ToString()));

                    x = 21 + rown * rpl; y = 4;
                    exAgent.SetCellValue(x, y, art.Trim());// + var.Trim() + artdes.Trim());
                    x = 21 + rown * rpl; y = 12;
                    if (color.Trim() == "000000")
                        color = "";
                    exAgent.SetCellValue(x, y, color.Trim() + " " + colordes.Trim() + cref);

                    x = 21 + rown * rpl; y = 20;
                    exAgent.SetCellValue(x, y, qty);

                    x = 21 + rown * rpl; y = 25;
                    exAgent.SetCellValue(x, y, um);
                    x = 21 + rown * rpl; y = 27;
                    exAgent.SetCellValue(x, y, price);

                    x = 21 + rown * rpl; y = 31;
                    exAgent.SetCellValue(x, y, tvalue);
                    //FillInQP(x + 3, art, var);
                    if (rown < count - 1)
                    {
                        exAgent.RangeCopy(21, 21 + rpl - 1, 21 + (rown + 1) * rpl, 21 + (rown + 1) * rpl + rpl);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FillInQP(int xstart, string art, string var)
        {
            string filter = "QAPART='" + art + "' and QAPVAR='" + var + "' ";
            DataRow[] rows = dsSource.Tables["QPPROF"].Select(filter);
            int x, y;
            x = xstart;
            exAgent.SetCellValue(x - 1, 4, "CV%");
            exAgent.SetCellValue(x - 1, 8, "-50%细节");
            exAgent.SetCellValue(x - 1, 12, "50%粗节");
            exAgent.SetCellValue(x - 1, 16, "+200%棉结");
            exAgent.SetCellValue(x - 1, 20, "实测强力");
            exAgent.SetCellValue(x - 1, 25, "低强");
            exAgent.SetCellValue(x - 1, 27, "纱支偏差");
            foreach (DataRow rx in rows)
            {
                //QAPANFART	QAPME	QAPMIN	QAPMAX
                //string term = Helper.GetQATerm(dsSource, rx["QAPANFART"].ToString());
                string term = rx["QAPANFART"].ToString();
                string me = rx["QAPME"].ToString();
                string min = (System.Convert.ToDecimal(rx["QAPMIN"].ToString()) / 1000).ToString();
                string max = (System.Convert.ToDecimal(rx["QAPMAX"].ToString()) / 1000).ToString();
                string value = (System.Convert.ToDecimal(rx["QAPBEM"].ToString()) / 10000).ToString();
                string scope = min + "~" + max;// +" " + me;
                if (value == "0")
                    value = scope + " ";// +me; 
                switch (term.Trim())
                {
                    case "CV":
                        y = 4;
                        exAgent.SetCellValue(x, y, value);
                        break;
                    case "XJ":
                        y = 8;
                        exAgent.SetCellValue(x, y, value);
                        break;
                    case "CJ":
                        y = 12;
                        exAgent.SetCellValue(x, y, value);
                        break;
                    case "MJ":
                        y = 16;
                        exAgent.SetCellValue(x, y, value);
                        break;
                    case "JQ":
                        y = 20;
                        exAgent.SetCellValue(x, y, value);
                        break;
                    case "DQ":
                        y = 25;
                        exAgent.SetCellValue(x, y, value);
                        break;
                    case "PC":
                        y = 27;
                        exAgent.SetCellValue(x, y, value);
                        break;
                }
            }
        }
    }
}
